function [ X_out,FX_out ] = updateGRInterval(X,FX,f)
%updateInterval Updates interval for golden ratio search
%   Detailed explanation goes here
% inputs:
%   X:      Set of three values
%   FX:     Function evalulated at X
%   F:      Function Handle

%Ratio of lengths of interval (a/b)
% |-----a-----|--b--|
R=(1+sqrt(5))/2;

% a/(a+b) is:
r=(-1+sqrt(5))/2;

%Find if the large interval is on right or left
if (X(2)-X(1)) > (X(3)-X(2))
     %X1-----X2---X3
    IntSide='Left';    
else 
     %X1---X2-----X3
    IntSide='Right'; 
end

switch IntSide
    case 'Left'
        %X1---Xnew--X2---X3
        
        %Find new X position
        IntSize=X(2)-X(1); %large interval size
        Xnew=X(1)+IntSize*r;
        %Evaulate function at Xnew
        F_Xnew=f(Xnew);
        
        %Return new interval X and FX depending on F_Xnew
        if F_Xnew<FX(2)  %use left interval
            X_out(1)=X(1);
            X_out(2)=Xnew;
            X_out(3)=X(2);
            FX_out(1)=FX(1);
            FX_out(2)=F_Xnew;
            FX_out(3)=FX(2);
        else    %use right interval
            X_out(1)=Xnew;
            X_out(2)=X(2);
            X_out(3)=X(3);
            FX_out(1)=F_Xnew;
            FX_out(2)=FX(2);
            FX_out(3)=FX(3);
        end
        
    case 'Right'
        
        %X1---X2--Xnew---X3
        
        %Find new X position
        IntSize=X(3)-X(2); %large interval size
        Xnew=X(2)+IntSize*(1-r);
        %Evaulate function at Xnew
        F_Xnew=f(Xnew);
        
        %Return new interval X and FX depending on F_Xnew
        if F_Xnew<FX(2)  %use right interval
            X_out(1)=X(2);
            X_out(2)=Xnew;
            X_out(3)=X(3);
            FX_out(1)=FX(2);
            FX_out(2)=F_Xnew;
            FX_out(3)=FX(3);
        else    %use left interval
            X_out(1)=X(1);
            X_out(2)=X(2);
            X_out(3)=Xnew;
            FX_out(1)=FX(1);
            FX_out(2)=FX(2);
            FX_out(3)=F_Xnew;
        end
        
        
end




end

